//% color="#4f67d5" iconWidth=50 iconHeight=40
namespace NFCModule{
    //% block="Initialize the NFC module by I2C at address 0x24" blockType="command" 
    export function initNFCModuleByI2C(parameter: any, block: any) {
        Generator.addImport('from dfrobot_pn532 import PN532_I2C');
		Generator.addCode(`p_nfc = PN532_I2C()`);
		Generator.addCode(`p_nfc.begin()`);
    }

    //% block="Write [VALUE] to byte [BYTES] of Data Block [DATABLOCK]" blockType="command" 
    //% VALUE.shadow="range"   VALUE.params.min=0    VALUE.params.max=255    VALUE.defl=200
    //% BYTES.shadow="dropdown" BYTES.options="BYTES" 
    //% DATABLOCK.shadow="dropdown" DATABLOCK.options="DATABLOCK" 
    export function writeToDataBlock(parameter: any, block: any) {
        let datablock = parameter.DATABLOCK.code;
        let index = parameter.BYTES.code;
        let data = parameter.VALUE.code;
        Generator.addCode(`p_nfc.write_index_data(${datablock}, ${index}, ${data})`);
    }

    //% block="Read all data of Data Block [DATABLOCK] (return string)" blockType="reporter" 
    //% DATABLOCK.shadow="dropdown" DATABLOCK.options="DATABLOCK" 
    export function readAllOfDataBlock(parameter: any, block: any) {
        let datablock = parameter.DATABLOCK.code;
        Generator.addCode(`p_nfc.read_data(${datablock})`);
    }

    //% block="Read data at byte [BYTES] of Data Block [DATABLOCK] (return number)" blockType="reporter" 
    //% BYTES.shadow="dropdown" BYTES.options="BYTES" 
    //% DATABLOCK.shadow="dropdown" DATABLOCK.options="DATABLOCK" 
    export function readByteOfDataBlock(parameter: any, block: any) {
        let datablock = parameter.DATABLOCK.code;
        let index = parameter.BYTES.code;
        Generator.addCode(`p_nfc.read_data(${datablock}, ${index})`);
    }

    //% block="Read UID of Card(return string)" blockType="reporter" 
    export function readUID(parameter: any, block: any) {
        Generator.addCode(`p_nfc.read_uid()`);
    }

    //% block="Detected card with UID [UID]" blockType="boolean" 
    //% UID.shadow="string"   UID.defl="4978ef9c"
    export function detectCardUID(parameter: any, block: any) {
        let uid = parameter.UID.code;
        Generator.addCode(`p_nfc.scan(${uid})`);
    }

    //% block="Detected card" blockType="boolean" 
    export function detectCard(parameter: any, block: any) {
        Generator.addCode(`p_nfc.scan()`);
    }
}
    